package com.aw.baselib.widgets.font

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import com.aw.baselib.R
import com.aw.baselib.base.AwBaseApplication

/**
 * @author : hzw
 * https://blog.csdn.net/yuanxw44/article/details/80019501
 */
@SuppressLint("AppCompatCustomView")
class AwTvRegularCenterImg(context: Context?, attrs: AttributeSet?) : AwTvRegularBase(
    context!!, attrs
) {
    override fun setViewBg() {
        background = resources.getDrawable(R.drawable.bg_common_input_empty)
    }

    init {
        typeface = AwBaseApplication.instance?.typefaceRegular
    }

    override fun onDraw(canvas: Canvas) {
        val drawables = compoundDrawables
        if (drawables != null) {
            val drawableLeft = drawables[0]
            if (drawableLeft != null) {
                val textWidth = paint.measureText(text.toString())
                val drawablePadding = compoundDrawablePadding
                var drawableWidth = 0
                drawableWidth = drawableLeft.intrinsicWidth
                val bodyWidth = textWidth + drawableWidth + drawablePadding
                canvas.translate((width - bodyWidth) / 2, 0f)
            }
        }
        super.onDraw(canvas)
    }
}